home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / cmln0286.arc / FILETEST.C < prev    next >
Text File  |  1986-02-03  |  3KB  |  128 lines

  1. /************************************************************************
  2. *                                    *
  3. *    filetest.c                            *
  4. *                                    *
  5. *    Example Program to test file reading and writing in C.        *
  6. *                                    *
  7. *    This program asks for the name of an input file and output    *
  8. *    file and copies from input to output.                *
  9. *                                    *
  10. *    A. Skjellum                            *
  11. *    November 29, 1985                        *
  12. *                                    *
  13. *                                    *
  14. *    Computer Language Magazine [C Interpreter Wrap-Up, Feb., 1986]    *
  15. *                                    *
  16. ************************************************************************/
  17.  
  18. #include <stdio.h>
  19. /*
  20. #define    LIVINGC 1
  21. */
  22. #ifdef    LIVINGC
  23. #define    fgetc getc
  24. #define    fputc putc
  25. #endif
  26.  
  27. #define FNAME    75    /* length of file name strings */
  28.  
  29. main()
  30. {
  31.     printf("filetest.c: copies input file to output     [29-Nov-85]\n\n");
  32.  
  33.     do
  34.     {
  35.         mainloop();
  36.     }
  37.     while (yesno("Another copy? "));
  38.  
  39.     printf("\n\nEnd of Execution.\n");            
  40. }
  41.  
  42. mainloop()
  43. {
  44.     int chr;    /* character by character file copy */
  45.  
  46.     FILE *input, *output;  /* handles for input and output files */
  47.  
  48.     char istring[FNAME];
  49.     char ostring[FNAME];    /* strings for file names */
  50.  
  51.     input  = NULL;
  52.     output = NULL;        /* indicate nothing opened in either */
  53.  
  54.     while (input == NULL)    /* try until we get an existing file */
  55.     {
  56.         printf("\n\nEnter name of input file: ");
  57.         scanf("%s",istring);
  58.  
  59.         input = fopen(istring,"r");
  60.  
  61.         if (input == NULL)
  62.             printf("File not found: %s\n",istring);
  63.         
  64.     }
  65.  
  66.     /* now see if output file already exists: */
  67.  
  68.     while (output == NULL)    /* try until we get a legal output name */
  69.     {
  70.         printf("\n\nEnter name of output file: ");
  71.         scanf("%s",ostring);
  72.  
  73.         if ((output = fopen(ostring,"r")) != NULL) /* exists? */
  74.         {
  75.             fclose(output);        /* close it */
  76.  
  77.             printf("Output file %s already exists",ostring);
  78.  
  79.             if (yesno("Overwrite? ")) /* dump old file */
  80.                 unlink(ostring); /* delete it */
  81.             else
  82.             {        /* start over: */
  83.                 printf("Start over:");
  84.                 output = NULL; /* reset loop condition */
  85.                 continue;
  86.             }
  87.  
  88.         }
  89.  
  90.         if ((output = fopen(ostring,"w")) == NULL)
  91.             printf("Cannot open %s for output\n",ostring);
  92.  
  93.     } /* end while(output == NULL) */
  94.  
  95.     /* ready to copy */
  96.  
  97.     while ((chr = fgetc(input)) != EOF)
  98.         fputc(chr,output);
  99.  
  100.     fclose(input);
  101.     fclose(output);        /* close both active files */
  102.  
  103. }
  104.  
  105. yesno(prompt)    /* robust yesno input routine */
  106. char *prompt;
  107. {
  108.     char chr;
  109.  
  110.     do    /* prompt + character input: */
  111.     {
  112.         printf("\n%s",prompt);      /* echo prompt */
  113.  
  114.         do    /* white space disposal */
  115.         {
  116.             chr = tolower(getchar()); /* lowercase it */
  117.         }
  118.         while(isspace(chr));
  119.     }
  120.     while    /* data verification: */
  121.     (
  122.         (chr != 'y') &&    /* yes entered */
  123.         (chr != 'n')    /* no  entered */
  124.     );
  125.  
  126.     return((chr == 'y') ? 1 : 0);    /* 1 --> true, 0 --> false */
  127. }
  128.